package com.gentle.platform.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.gentle.platform.domain.FmUserForm;
import com.gentle.platform.service.IFmUserFormService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gentle.common.annotation.Log;
import com.gentle.common.core.controller.BaseController;
import com.gentle.common.core.domain.AjaxResult;
import com.gentle.common.enums.BusinessType;
import com.gentle.common.utils.poi.ExcelUtil;
import com.gentle.common.core.page.TableDataInfo;

/**
 * 调查问卷
 * 
 * @author XNHE
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/plat")
public class FmUserFormController extends BaseController
{
    @Autowired
    private IFmUserFormService fmUserFormService;

    /**
     * 查询我的表单分页
     */
    @PreAuthorize("@ss.hasPermi('system:form:list')")
    @GetMapping("/user/form/page")
    @ApiOperation(value = "查询我的表单分页")
    public AjaxResult queryMyForms(@RequestParam("current") Integer current,
                                   @RequestParam("size") Integer size,
                                   @RequestParam("folderId") Integer folderId) {
//        http://localhost/dev-api/plat/user/form/page?current=1&size=12&folderId=0
        // 获取当前登陆人信息
        Long userId = getUserId();
        System.out.println("userId = " + userId);
        String jsonStr = "{\"data\":{\"records\":[{\"id\":8468,\"createTime\":\"2024-10-24 14:18:29\",\"updateTime\":\"2024-10-24 14:28:47\",\"formKey\":\"W0iYPPsy\",\"name\":\"<p>宝安区税务局“专精特新”企业调查问卷</p>\",\"description\":\"<p style=\\\"text-align: center;\\\">（本问卷仅供税收机关内部使用，请放心填写）</p>\",\"sourceType\":1,\"sourceTypeDesc\":\"空白创建\",\"sourceId\":null,\"userId\":1,\"status\":2,\"statusDesc\":\"收集中\",\"type\":1,\"typeDesc\":\"普通表单\",\"deleted\":false,\"folder\":false,\"folderId\":0,\"textName\":\"宝安区税务局“专精特新”企业调查问卷\"}],\"total\":1,\"size\":12,\"current\":1,\"orders\":[],\"optimizeCountSql\":true,\"searchCount\":true,\"maxLimit\":null,\"countId\":null,\"pages\":1}}";
        jsonStr = "{\"records\":[{\"id\":8468,\"createTime\":\"2024-10-24 14:18:29\",\"updateTime\":\"2024-10-24 14:28:47\",\"formKey\":\"W0iYPPsy\",\"name\":\"<p>宝安区税务局“专精特新”企业调查问卷</p>\",\"description\":\"<p style=\\\"text-align: center;\\\">（本问卷仅供税收机关内部使用，请放心填写）</p>\",\"sourceType\":1,\"sourceTypeDesc\":\"空白创建\",\"sourceId\":null,\"userId\":1,\"status\":2,\"statusDesc\":\"收集中\",\"type\":1,\"typeDesc\":\"普通表单\",\"deleted\":false,\"folder\":false,\"folderId\":0,\"textName\":\"宝安区税务局“专精特新”企业调查问卷\"}],\"total\":1,\"size\":12,\"current\":1,\"orders\":[],\"optimizeCountSql\":true,\"searchCount\":true,\"maxLimit\":null,\"countId\":null,\"pages\":1}";

        JSONObject data = JSONObject.parseObject(jsonStr);
        return success(data) ;
    }

    /**
     * 查询用户单列表
     */
    @PreAuthorize("@ss.hasPermi('system:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmUserForm fmUserForm)
    {
        startPage();
        List<FmUserForm> list = fmUserFormService.selectFmUserFormList(fmUserForm);
        return getDataTable(list);
    }

    /**
     * 导出用户单列表
     */
    @PreAuthorize("@ss.hasPermi('system:form:export')")
    @Log(title = "用户单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmUserForm fmUserForm)
    {
        List<FmUserForm> list = fmUserFormService.selectFmUserFormList(fmUserForm);
        ExcelUtil<FmUserForm> util = new ExcelUtil<FmUserForm>(FmUserForm.class);
        util.exportExcel(response, list, "用户单数据");
    }

    /**
     * 获取用户单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:form:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fmUserFormService.selectFmUserFormById(id));
    }

    /**
     * 新增用户单
     */
    @PreAuthorize("@ss.hasPermi('system:form:add')")
    @Log(title = "用户单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FmUserForm fmUserForm)
    {
        return toAjax(fmUserFormService.insertFmUserForm(fmUserForm));
    }

    /**
     * 修改用户单
     */
    @PreAuthorize("@ss.hasPermi('system:form:edit')")
    @Log(title = "用户单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FmUserForm fmUserForm)
    {
        return toAjax(fmUserFormService.updateFmUserForm(fmUserForm));
    }

    /**
     * 删除用户单
     */
    @PreAuthorize("@ss.hasPermi('system:form:remove')")
    @Log(title = "用户单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fmUserFormService.deleteFmUserFormByIds(ids));
    }
}
